/*
 * @(#)MtmWorkflowDynamicUserActor.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.workflow.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.user.bean.UserGroup;
import com.dcivision.user.bean.UserRole;
import com.dcivision.user.dao.UserGroupDAObject;
import com.dcivision.user.dao.UserRoleDAObject;
import com.dcivision.workflow.bean.MtmWorkflowDynamicUserActor;
import com.dcivision.workflow.bean.MtmWorkflowStepUserActor;
import com.dcivision.workflow.bean.WorkflowProgress;

/**
 * MtmWorkflowDynamicUserActorDAObject.java
 *
 * This class is the data access bean for table "MTM_WF_DYNAMIC_USER_ACTOR".
 * 
 * @author Tony Chen
 * @company DCIVision Limited
 * @creation date 04/11/2004
 * @version $Revision: 1.9 $
*/
public class MtmWorkflowDynamicUserActorDAObject extends AbstractDAObject {

  public static final String REVISION = "$Revision: 1.9 $";

  public static final String TABLE_NAME = "MTM_WF_DYNAMIC_USER_ACTOR";

  public MtmWorkflowDynamicUserActorDAObject(SessionContainer sessionContainer, Connection dbConn) {
    super(sessionContainer, dbConn);
  }

  protected void initDBSetting() {
    this.baseTableName = TABLE_NAME;
    this.vecDBColumn.add("ID");
    this.vecDBColumn.add("WORKFLOW_PROGRESS_ID");
    this.vecDBColumn.add("ACTOR_TYPE");
    this.vecDBColumn.add("ACTOR_ID");
    this.vecDBColumn.add("PERMISSION");
    this.vecDBColumn.add("RECORD_STATUS");
    this.vecDBColumn.add("UPDATE_COUNT");
    this.vecDBColumn.add("CREATOR_ID");
    this.vecDBColumn.add("CREATE_DATE");
    this.vecDBColumn.add("UPDATER_ID");
    this.vecDBColumn.add("UPDATE_DATE");
  }

  protected synchronized AbstractBaseObject getByID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_PROGRESS_ID, A.ACTOR_TYPE, A.PERMISSION, A.ACTOR_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_DYNAMIC_USER_ACTOR A ");
        sqlStat.append("WHERE  A.ID = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        if (rs.next()) {
          MtmWorkflowDynamicUserActor tmpMtmWorkflowDynamicUserActor = new MtmWorkflowDynamicUserActor();
          tmpMtmWorkflowDynamicUserActor.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWorkflowDynamicUserActor.setWorkflowProgressID(getResultSetInteger(rs, "WORKFLOW_PROGRESS_ID"));
          tmpMtmWorkflowDynamicUserActor.setActorType(getResultSetString(rs, "ACTOR_TYPE"));
          tmpMtmWorkflowDynamicUserActor.setPermission(getResultSetString(rs, "PERMISSION"));
          tmpMtmWorkflowDynamicUserActor.setActorID(getResultSetInteger(rs, "ACTOR_ID"));
          tmpMtmWorkflowDynamicUserActor.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWorkflowDynamicUserActor.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowDynamicUserActor.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowDynamicUserActor.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowDynamicUserActor.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowDynamicUserActor.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowDynamicUserActor.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowDynamicUserActor.getCreatorID()));
          tmpMtmWorkflowDynamicUserActor.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowDynamicUserActor.getUpdaterID()));
          return(tmpMtmWorkflowDynamicUserActor);
        } else {
          throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized List getList(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_PROGRESS_ID, A.ACTOR_TYPE, A.PERMISSION, A.ACTOR_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_DYNAMIC_USER_ACTOR A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");

        // Form the WHERE clause for filtering.
        if (searchForm.isSearchable()) {
          String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }

        //format the sql for any 'LIKE' statement contained
        sqlStat = this.getFormattedSQL(sqlStat.toString());

        // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }

        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStatCnt, 2, searchKeyword);
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStat, 2, searchKeyword);
        }
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt<pageSize) {
          MtmWorkflowDynamicUserActor tmpMtmWorkflowDynamicUserActor = new MtmWorkflowDynamicUserActor();
          tmpMtmWorkflowDynamicUserActor.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWorkflowDynamicUserActor.setWorkflowProgressID(getResultSetInteger(rs, "WORKFLOW_PROGRESS_ID"));
          tmpMtmWorkflowDynamicUserActor.setActorType(getResultSetString(rs, "ACTOR_TYPE"));
          tmpMtmWorkflowDynamicUserActor.setActorID(getResultSetInteger(rs, "ACTOR_ID"));
          tmpMtmWorkflowDynamicUserActor.setPermission(getResultSetString(rs, "PERMISSION"));
          tmpMtmWorkflowDynamicUserActor.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWorkflowDynamicUserActor.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowDynamicUserActor.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowDynamicUserActor.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowDynamicUserActor.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowDynamicUserActor.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowDynamicUserActor.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowDynamicUserActor.getCreatorID()));
          tmpMtmWorkflowDynamicUserActor.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowDynamicUserActor.getUpdaterID()));
          tmpMtmWorkflowDynamicUserActor.setRecordCount(totalNumOfRecord);
          tmpMtmWorkflowDynamicUserActor.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpMtmWorkflowDynamicUserActor);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
      }
    }
  }

  protected synchronized List getList() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_PROGRESS_ID, A.ACTOR_TYPE, A.PERMISSION, A.ACTOR_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_DYNAMIC_USER_ACTOR A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmWorkflowDynamicUserActor tmpMtmWorkflowDynamicUserActor = new MtmWorkflowDynamicUserActor();
          tmpMtmWorkflowDynamicUserActor.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWorkflowDynamicUserActor.setWorkflowProgressID(getResultSetInteger(rs, "WORKFLOW_PROGRESS_ID"));
          tmpMtmWorkflowDynamicUserActor.setActorType(getResultSetString(rs, "ACTOR_TYPE"));
          tmpMtmWorkflowDynamicUserActor.setActorID(getResultSetInteger(rs, "ACTOR_ID"));
          tmpMtmWorkflowDynamicUserActor.setPermission(getResultSetString(rs, "PERMISSION"));
          tmpMtmWorkflowDynamicUserActor.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWorkflowDynamicUserActor.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowDynamicUserActor.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowDynamicUserActor.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowDynamicUserActor.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowDynamicUserActor.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowDynamicUserActor.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowDynamicUserActor.getCreatorID()));
          tmpMtmWorkflowDynamicUserActor.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowDynamicUserActor.getUpdaterID()));
          result.add(tmpMtmWorkflowDynamicUserActor);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  public synchronized void copyRecordByWorkflowProgressID(Integer oldProgressID, Integer newProgressID) throws ApplicationException {
      PreparedStatement preStat = null;
      ResultSet rs = null;
      StringBuffer sqlStat = new StringBuffer();
      List result = new ArrayList();

      synchronized(dbConn) {
        try {
          sqlStat.append("SELECT A.ID, A.WORKFLOW_PROGRESS_ID, A.ACTOR_TYPE, A.PERMISSION,  A.ACTOR_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
          sqlStat.append("FROM   MTM_WF_DYNAMIC_USER_ACTOR A ");
          sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.WORKFLOW_PROGRESS_ID = ? ");
          preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
          this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
          this.setPrepareStatement(preStat, 2, oldProgressID);
          rs = preStat.executeQuery();
          while (rs.next()) {
            MtmWorkflowDynamicUserActor tmpMtmWorkflowDynamicUserActor = new MtmWorkflowDynamicUserActor();
            tmpMtmWorkflowDynamicUserActor.setWorkflowProgressID(newProgressID);
            tmpMtmWorkflowDynamicUserActor.setActorType(getResultSetString(rs, "ACTOR_TYPE"));
            tmpMtmWorkflowDynamicUserActor.setActorID(getResultSetInteger(rs, "ACTOR_ID"));
            tmpMtmWorkflowDynamicUserActor.setPermission(getResultSetString(rs, "PERMISSION"));
            tmpMtmWorkflowDynamicUserActor.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
            tmpMtmWorkflowDynamicUserActor.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
            tmpMtmWorkflowDynamicUserActor.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
            tmpMtmWorkflowDynamicUserActor.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
            tmpMtmWorkflowDynamicUserActor.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
            tmpMtmWorkflowDynamicUserActor.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
            tmpMtmWorkflowDynamicUserActor.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowDynamicUserActor.getCreatorID()));
            tmpMtmWorkflowDynamicUserActor.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowDynamicUserActor.getUpdaterID()));
            this.insertObject(tmpMtmWorkflowDynamicUserActor);
          }
        } catch (ApplicationException appEx) {
          throw appEx;
        } catch (SQLException sqle) {
          log.error(sqle, sqle);
          throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
        } catch (Exception e) {
          log.error(e, e);
          throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
        } finally {
          try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
          try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        }
      }
    }
    
  

  protected void validateInsert(AbstractBaseObject obj) throws ApplicationException {
  }

  protected void validateUpdate(AbstractBaseObject obj) throws ApplicationException {
  }

  protected void validateDelete(AbstractBaseObject obj) throws ApplicationException {
  }

  protected synchronized AbstractBaseObject insert(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmWorkflowDynamicUserActor tmpMtmWorkflowDynamicUserActor = (MtmWorkflowDynamicUserActor)((MtmWorkflowDynamicUserActor)obj).clone();

    synchronized(dbConn) {
      try {
        Integer nextID = getNextPrimaryID();
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("INSERT ");
        sqlStat.append("INTO   MTM_WF_DYNAMIC_USER_ACTOR(ID, WORKFLOW_PROGRESS_ID, ACTOR_TYPE, PERMISSION, ACTOR_ID, RECORD_STATUS, UPDATE_COUNT, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE) ");
        sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, nextID);
        setPrepareStatement(preStat, 2, tmpMtmWorkflowDynamicUserActor.getWorkflowProgressID());
        setPrepareStatement(preStat, 3, tmpMtmWorkflowDynamicUserActor.getActorType());
        setPrepareStatement(preStat, 4, tmpMtmWorkflowDynamicUserActor.getPermission());
        setPrepareStatement(preStat, 5, tmpMtmWorkflowDynamicUserActor.getActorID());

        setPrepareStatement(preStat, 6, GlobalConstant.RECORD_STATUS_ACTIVE);
        setPrepareStatement(preStat, 7, new Integer(0));
        setPrepareStatement(preStat, 8, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 9, currTime);
        setPrepareStatement(preStat, 10, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 11, currTime);
        preStat.executeUpdate();
        tmpMtmWorkflowDynamicUserActor.setID(nextID);
        tmpMtmWorkflowDynamicUserActor.setCreatorID(sessionContainer.getUserRecordID());
        tmpMtmWorkflowDynamicUserActor.setCreateDate(currTime);
        tmpMtmWorkflowDynamicUserActor.setUpdaterID(sessionContainer.getUserRecordID());
        tmpMtmWorkflowDynamicUserActor.setUpdateDate(currTime);
        tmpMtmWorkflowDynamicUserActor.setUpdateCount(new Integer(0));
        tmpMtmWorkflowDynamicUserActor.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowDynamicUserActor.getCreatorID()));
        tmpMtmWorkflowDynamicUserActor.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowDynamicUserActor.getUpdaterID()));
        return(tmpMtmWorkflowDynamicUserActor);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_INSERT_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized AbstractBaseObject update(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmWorkflowDynamicUserActor tmpMtmWorkflowDynamicUserActor = (MtmWorkflowDynamicUserActor)((MtmWorkflowDynamicUserActor)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE MTM_WF_DYNAMIC_USER_ACTOR ");
        sqlStat.append("SET  WORKFLOW_PROGRESS_ID=?, ACTOR_TYPE=?, PERMISSION=?, ACTOR_ID=?, UPDATE_COUNT=?, UPDATER_ID=?, UPDATE_DATE=? ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpMtmWorkflowDynamicUserActor.getWorkflowProgressID());
        setPrepareStatement(preStat, 2, tmpMtmWorkflowDynamicUserActor.getActorType());
        setPrepareStatement(preStat, 3, tmpMtmWorkflowDynamicUserActor.getPermission());
        setPrepareStatement(preStat, 4, tmpMtmWorkflowDynamicUserActor.getActorID());
        setPrepareStatement(preStat, 5, new Integer(tmpMtmWorkflowDynamicUserActor.getUpdateCount().intValue() + 1));
        setPrepareStatement(preStat, 6, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 7, currTime);
        setPrepareStatement(preStat, 8, tmpMtmWorkflowDynamicUserActor.getID());
        setPrepareStatement(preStat, 9, tmpMtmWorkflowDynamicUserActor.getUpdateCount());

        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          tmpMtmWorkflowDynamicUserActor.setUpdaterID(sessionContainer.getUserRecordID());
          tmpMtmWorkflowDynamicUserActor.setUpdateDate(currTime);
          tmpMtmWorkflowDynamicUserActor.setUpdateCount(new Integer(tmpMtmWorkflowDynamicUserActor.getUpdateCount().intValue() + 1));
          tmpMtmWorkflowDynamicUserActor.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowDynamicUserActor.getCreatorID()));
          tmpMtmWorkflowDynamicUserActor.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowDynamicUserActor.getUpdaterID()));
          return(tmpMtmWorkflowDynamicUserActor);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized AbstractBaseObject delete(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmWorkflowDynamicUserActor tmpMtmWorkflowDynamicUserActor = (MtmWorkflowDynamicUserActor)((MtmWorkflowDynamicUserActor)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   MTM_WF_DYNAMIC_USER_ACTOR ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpMtmWorkflowDynamicUserActor.getID());
        setPrepareStatement(preStat, 2, tmpMtmWorkflowDynamicUserActor.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          return(tmpMtmWorkflowDynamicUserActor);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized void auditTrail(String opMode, AbstractBaseObject obj) throws ApplicationException {
    Vector oldValues = new Vector();
    Vector newValues = new Vector();
    MtmWorkflowDynamicUserActor tmpMtmWorkflowDynamicUserActor = (MtmWorkflowDynamicUserActor)this.oldValue;

    if (tmpMtmWorkflowDynamicUserActor != null) {
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowDynamicUserActor.getID()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowDynamicUserActor.getWorkflowProgressID()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowDynamicUserActor.getActorType()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowDynamicUserActor.getActorID()));

      oldValues.add(toAuditTrailValue(tmpMtmWorkflowDynamicUserActor.getRecordStatus()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowDynamicUserActor.getUpdateCount()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowDynamicUserActor.getCreatorID()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowDynamicUserActor.getCreateDate()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowDynamicUserActor.getUpdaterID()));
      oldValues.add(toAuditTrailValue(tmpMtmWorkflowDynamicUserActor.getUpdateDate()));
    }

    tmpMtmWorkflowDynamicUserActor = (MtmWorkflowDynamicUserActor)obj;
    if (tmpMtmWorkflowDynamicUserActor != null) {
      newValues.add(toAuditTrailValue(tmpMtmWorkflowDynamicUserActor.getID()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowDynamicUserActor.getWorkflowProgressID()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowDynamicUserActor.getActorType()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowDynamicUserActor.getActorID()));

      newValues.add(toAuditTrailValue(tmpMtmWorkflowDynamicUserActor.getRecordStatus()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowDynamicUserActor.getUpdateCount()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowDynamicUserActor.getCreatorID()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowDynamicUserActor.getCreateDate()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowDynamicUserActor.getUpdaterID()));
      newValues.add(toAuditTrailValue(tmpMtmWorkflowDynamicUserActor.getUpdateDate()));
    }

    auditTrailBase(opMode, oldValues, newValues);
  }

  /***********************************************************************
   * DON'T Modify the codes above unless you know what you are doing!!!  *
   * Put your own functions beblow.                                      *
   * For FINDER methods, the function name should be in the notation:    *
   *   public Object getObjectBy<Criteria>()                             *
   *   - e.g. public Object getObjectByCode()                            *
   *   public List getListBy<Criteria>()                                 *
   *   - e.g. public List getListByUserID()                              *
   * For OPERATION methods, the function name should be in the notation: *
   *   public void <Operation>ObjectBy<Criteria>()                       *
   *   - e.g. public void deleteObjectByCode()                           *
   *   public void <Operation>ListBy<Criteria>()                         *
   *   - e.g. public void deleteListByUserID()                           *
   ***********************************************************************/
    
  /**
   * Gets a list of dynamically assigned as progress owner of the given workflow progress
   * 
   * @param workflowProgressID The workflow progress ID
   * @return List of MtmWorkflowDynamicUserActor objects
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.WorkflowProgress
   * @see com.dcivision.workflow.bean.MtmWorkflowDynamicUserActor
   */  
  public synchronized List getListByWorkflowProgressID(Integer workflowProgressID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    UserGroupDAObject groupDAO = new UserGroupDAObject(this.sessionContainer,this.dbConn);
    UserRoleDAObject roleDAO = new UserRoleDAObject(this.sessionContainer,this.dbConn);
    
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_PROGRESS_ID, A.ACTOR_TYPE, A.PERMISSION, A.ACTOR_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_WF_DYNAMIC_USER_ACTOR A ");
        sqlStat.append("WHERE  A.WORKFLOW_PROGRESS_ID = ? AND A.RECORD_STATUS = ? ");
        sqlStat.append("ORDER BY A.ID DESC ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, workflowProgressID);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmWorkflowDynamicUserActor tmpMtmWorkflowDynamicUserActor = new MtmWorkflowDynamicUserActor();
          tmpMtmWorkflowDynamicUserActor.setID(getResultSetInteger(rs, "ID"));
          tmpMtmWorkflowDynamicUserActor.setWorkflowProgressID(getResultSetInteger(rs, "WORKFLOW_PROGRESS_ID"));
          tmpMtmWorkflowDynamicUserActor.setActorType(getResultSetString(rs, "ACTOR_TYPE"));
          tmpMtmWorkflowDynamicUserActor.setActorID(getResultSetInteger(rs, "ACTOR_ID"));
          tmpMtmWorkflowDynamicUserActor.setPermission(getResultSetString(rs, "PERMISSION"));
          tmpMtmWorkflowDynamicUserActor.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmWorkflowDynamicUserActor.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmWorkflowDynamicUserActor.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmWorkflowDynamicUserActor.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmWorkflowDynamicUserActor.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmWorkflowDynamicUserActor.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmWorkflowDynamicUserActor.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowDynamicUserActor.getCreatorID()));
          tmpMtmWorkflowDynamicUserActor.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowDynamicUserActor.getUpdaterID()));
          
          if(MtmWorkflowStepUserActor.ACTOR_TYPE_USER.equals(tmpMtmWorkflowDynamicUserActor.getActorType()) ){
            //Is User
            tmpMtmWorkflowDynamicUserActor.setActorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowDynamicUserActor.getActorID()));
            
          } else if(MtmWorkflowStepUserActor.ACTOR_TYPE_GROUP.equals(tmpMtmWorkflowDynamicUserActor.getActorType()) ){
            //Is Group
            UserGroup userGroup = (UserGroup)groupDAO.getObjectByID(tmpMtmWorkflowDynamicUserActor.getActorID());
            tmpMtmWorkflowDynamicUserActor.setActorName(userGroup.getGroupName());
          } else if(MtmWorkflowStepUserActor.ACTOR_TYPE_ROLE.equals(tmpMtmWorkflowDynamicUserActor.getActorType()) ){
            //Is Role
            UserRole userRole = (UserRole)roleDAO.getObjectByID(tmpMtmWorkflowDynamicUserActor.getActorID());
            tmpMtmWorkflowDynamicUserActor.setActorName(userRole.getRoleName());
          }                    
          result.add(tmpMtmWorkflowDynamicUserActor);
        } 
        return result;
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  /**
   * Gets a list of user record ID that is dynamically assigned as progress owner
   * of the given workflow progress
   * 
   * @param workflowProgressID The workflow progress ID
   * @return List of user record ID
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.WorkflowProgress
   * @see com.dcivision.user.bean.UserRecord
   */
  public synchronized List getDynamicUserIDListByWorkflowProgressID(Integer workflowProgressID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    UserGroupDAObject groupDAO = new UserGroupDAObject(this.sessionContainer,this.dbConn);
    UserRoleDAObject roleDAO = new UserRoleDAObject(this.sessionContainer,this.dbConn);
    
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT DISTINCT U.ID ");
        
        sqlStat.append("FROM MTM_WF_DYNAMIC_USER_ACTOR DUA, ");
        sqlStat.append("     USER_RECORD U ");
        sqlStat.append("     LEFT JOIN MTM_USER_RECORD_USER_GROUP UG ON U.ID = UG.USER_RECORD_ID AND UG.RECORD_STATUS = U.RECORD_STATUS ");
        sqlStat.append("     LEFT JOIN MTM_USER_RECORD_USER_ROLE UR ON U.ID = UR.USER_RECORD_ID AND UR.RECORD_STATUS = U.RECORD_STATUS ");
        sqlStat.append("     LEFT JOIN MTM_USER_GROUP_USER_ROLE GR ON UG.USER_GROUP_ID = GR.USER_GROUP_ID AND GR.RECORD_STATUS = U.RECORD_STATUS ");
        
        sqlStat.append("WHERE  DUA.RECORD_STATUS = ? ");
        sqlStat.append("AND    U.RECORD_STATUS = DUA.RECORD_STATUS ");
        sqlStat.append("AND    ( (DUA.ACTOR_TYPE = ? AND DUA.ACTOR_ID = U.ID) OR "); 
        sqlStat.append("         (DUA.ACTOR_TYPE = ? AND DUA.ACTOR_ID = UG.USER_GROUP_ID) OR "); 
        sqlStat.append("         (DUA.ACTOR_TYPE = ? AND (DUA.ACTOR_ID = GR.USER_ROLE_ID OR DUA.ACTOR_ID = UR.USER_ROLE_ID)) ");
        sqlStat.append("       ) ");        
        sqlStat.append("AND    DUA.WORKFLOW_PROGRESS_ID = ? ");
        
        
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, GlobalConstant.SUBJECT_TYPE_USER);
        this.setPrepareStatement(preStat, 3, GlobalConstant.SUBJECT_TYPE_GROUP);
        this.setPrepareStatement(preStat, 4, GlobalConstant.SUBJECT_TYPE_ROLE);
        this.setPrepareStatement(preStat, 5, workflowProgressID);
        
        rs = preStat.executeQuery();
        while (rs.next()) {         
          result.add(getResultSetInteger(rs, "ID"));
        } 
        return result;
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  public  synchronized List getListWorkflowStepIDByActorIDAndActorType(Integer actorID,String actorType) throws ApplicationException {
	    PreparedStatement preStat = null;
	    ResultSet rs = null;
	    StringBuffer sqlStat = new StringBuffer();
	    List result = new ArrayList();

	    synchronized(dbConn) {
	      try {
	        sqlStat.append("SELECT A.ID, A.WORKFLOW_PROGRESS_ID, A.ACTOR_TYPE, A.PERMISSION, A.ACTOR_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
	        sqlStat.append("FROM   MTM_WF_DYNAMIC_USER_ACTOR A, WORKFLOW_PROGRESS B ");
	        //sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.Actor_ID = ? AND A.ACTOR_TYPE = ? ");//simon_del
	        sqlStat.append("WHERE A.WORKFLOW_PROGRESS_ID=B.ID AND A.RECORD_STATUS = ? AND A.ACTOR_ID = ? AND A.ACTOR_TYPE = ? AND B.RECORD_STATUS = ? AND B.STATUS= ? ");//simon_add
	        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
	        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
	        this.setPrepareStatement(preStat, 2, actorID);
	        this.setPrepareStatement(preStat, 3, actorType);
	        this.setPrepareStatement(preStat, 4, GlobalConstant.RECORD_STATUS_ACTIVE);//simon_add
	        this.setPrepareStatement(preStat, 5, WorkflowProgress.STATUS_GENERAL_ACCOMPLISHMENT);//simon_add
	        rs = preStat.executeQuery();
	        while (rs.next()) {
	            MtmWorkflowDynamicUserActor tmpMtmWorkflowDynamicUserActor = new MtmWorkflowDynamicUserActor();
	            tmpMtmWorkflowDynamicUserActor.setID(getResultSetInteger(rs, "ID"));
	            tmpMtmWorkflowDynamicUserActor.setWorkflowProgressID(getResultSetInteger(rs, "WORKFLOW_PROGRESS_ID"));
	            tmpMtmWorkflowDynamicUserActor.setActorType(getResultSetString(rs, "ACTOR_TYPE"));
	            tmpMtmWorkflowDynamicUserActor.setActorID(getResultSetInteger(rs, "ACTOR_ID"));
	            tmpMtmWorkflowDynamicUserActor.setPermission(getResultSetString(rs, "PERMISSION"));
	            tmpMtmWorkflowDynamicUserActor.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
	            tmpMtmWorkflowDynamicUserActor.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
	            tmpMtmWorkflowDynamicUserActor.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
	            tmpMtmWorkflowDynamicUserActor.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
	            tmpMtmWorkflowDynamicUserActor.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
	            tmpMtmWorkflowDynamicUserActor.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
	            tmpMtmWorkflowDynamicUserActor.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmWorkflowDynamicUserActor.getCreatorID()));
	            tmpMtmWorkflowDynamicUserActor.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmWorkflowDynamicUserActor.getUpdaterID()));
	            result.add(tmpMtmWorkflowDynamicUserActor);
	        }
	        return(result);
	      } catch (ApplicationException appEx) {
	        throw appEx;
	      } catch (SQLException sqle) {
	        log.error(sqle, sqle);
	        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
	      } catch (Exception e) {
	        log.error(e, e);
	        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
	      } finally {
	        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
	        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
	      }
	    }
	  }
}

